package net.xdclass.listener;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.exception.BizException;
import net.xdclass.model.EventMessage;
import net.xdclass.service.TrafficService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * @author Allen
 * @version 1.0
 * @date 2022/12/7 11:57
 */
@Component
@Slf4j
@RabbitListener(queues = {"order.traffic.queue","traffic.free_init.queue"})
public class TrafficMQListener {

	@Resource
	private TrafficService trafficService;


	@RabbitHandler
	public void trafficHandler(EventMessage eventMessage, Message message, Channel channel){

		log.info("监听到消息trafficHandler:{}",eventMessage);

		try{

			trafficService.handleTrafficMessage(eventMessage);

		}catch (Exception e){
			log.error("消费者失败:{}", JSONUtil.toJsonStr(e));
			throw new BizException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
		}

		log.info("消费成功:{}",eventMessage);

	}

}
